/**
 * Apr 3, 2010
 */
package org.tomale.id.gis.preferences;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;
import org.tomale.id.gis.Activator;

/**
 * @author ferd
 *
 */
public class GISPreferencePage extends PreferencePage implements
		IWorkbenchPreferencePage {

	Spinner _rotateIncrement;
	Spinner _translateIncrement;
	
	public GISPreferencePage(){
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite comp = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);
		
		Label l;
		
		l = new Label(comp, SWT.NONE);
		l.setText("Rotate Increment");
		l.setLayoutData(new GridData());
		
		_rotateIncrement = new Spinner(comp, SWT.BORDER);
		_rotateIncrement.setMinimum(5);
		_rotateIncrement.setMaximum(90);
		_rotateIncrement.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				true,false));
		
		l = new Label(comp, SWT.NONE);
		l.setText("Pan Increment");
		l.setLayoutData(new GridData());
		
		_translateIncrement = new Spinner(comp, SWT.BORDER);
		_translateIncrement.setMinimum(5);
		_translateIncrement.setMaximum(100);
		_translateIncrement.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, 
				true, false));
		
		setPreferences();
		
		return comp;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	@Override
	public void init(IWorkbench workbench) {
		// TODO Auto-generated method stub

	}
	
	@Override
	protected void performDefaults() {
		_rotateIncrement.setSelection((int) PreferenceConstants.DEFAULT_ROTATE_INCREMENT*100);
		_translateIncrement.setSelection((int) PreferenceConstants.DEFAULT_PAN_INCREMENT*100);
		
		super.performDefaults();
	}

	private void initProviders(){
		IPreferenceStore store = getPreferenceStore();
	}
	
	private void setPreferences(){
		IPreferenceStore store =getPreferenceStore();
		_rotateIncrement.setSelection((int) store.getFloat(PreferenceConstants.ROTATE_INCREMENT)*100);
		_translateIncrement.setSelection((int) store.getFloat(PreferenceConstants.PAN_INCREMENT)*100);
	}
	
	private void save() {
		if(isValid()){
			IPreferenceStore store = getPreferenceStore();
			store.setValue(PreferenceConstants.ROTATE_INCREMENT, _rotateIncrement.getSelection()/100);
			store.setValue(PreferenceConstants.PAN_INCREMENT, _translateIncrement.getSelection()/100);
		}
	}

	@Override
	public boolean performOk() {
		save();
		return super.performOk();
	}

	@Override
	public boolean isValid() {
		return true;
	}

}
